/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.jinxin.platform.eleinspect.controller
 * @Filename: EleinspectLineManageController.java
 * @Description: TODO(EleinspectLineManageController)
 * @author: 金鑫智慧
 * @date: 2020-03-30
 * @version: V1.0
 * @Copyright: 2020 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdelecinspect.controller;


import com.alibaba.fastjson.JSONArray;
import com.jinxin.platform.cdelecinspect.mapper.pojo.EleinspectLineManage;
import com.jinxin.platform.cdelecinspect.mapper.pojo.EleinspectMappingPointLine;
import com.jinxin.platform.cdelecinspect.mapper.pojo.EleinspectPointManage;
import com.jinxin.platform.cdelecinspect.service.EleinspectLineManageService;
import com.jinxin.platform.cdelecinspect.service.EleinspectMappingPointLineService;
import com.jinxin.platform.cdelecinspect.service.EleinspectPointManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * TODO(EleinspectLineManageController)
 * @Classn: EleinspectLineManageController
 * @author Lynn
 * @date 2020-03-30
 */
@RestController
@RequestMapping("/eleinspect/line")
public class EleinspectLineManageController {
    @Autowired
    EleinspectLineManageService eleinspectLineManageService;
    @Autowired
    EleinspectPointManageService eleinspectPointManageService;
    @Autowired
    EleinspectMappingPointLineService eleinspectMappingPointLineService;

    @RequestMapping("/getLineTable")
    public Map<String,Object> getLineTable(Integer page,Integer limit,String lineName,String lineId){
        HashMap<String,Object> param = new HashMap<>();
        param.put("lineId", lineId);
        param.put("lineName", lineName);
        param.put("page", page);
        param.put("limit", limit);
        List<EleinspectLineManage> allLine = eleinspectLineManageService.listPage(param);
        int pageCount = eleinspectLineManageService.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        resultMap.put("data",allLine);
        resultMap.put("count",pageCount);
        return resultMap;
    }

    @RequestMapping("/getPointTable")
    public Map<String,Object> getPointTable(Integer page, Integer limit){
        //未被使用的点位
        int pointUsedStatus = 0;
        HashMap<String,Object> param = new HashMap<>();
        param.put("page", page );
        param.put("limit", limit);
//        param.put("pointUsedStatus",pointUsedStatus);
        List<EleinspectPointManage> allPoint = eleinspectPointManageService.listPage(param);
        int pageCount = eleinspectPointManageService.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        resultMap.put("data",allPoint);
        resultMap.put("count",pageCount);
        return resultMap;
    }

    @RequestMapping("/saveLineInfo")
    public Map<String,Object> saveLineInfo(EleinspectLineManage eleinspectLineManage,String firstInspectTime,String deletePointIdJson,int editIndex,String dataArrayJson){
        HashMap<String,Object> resultMap = new HashMap<>();
        if (editIndex == 0){
            eleinspectLineManage.setLineId(UUID.randomUUID().toString());
        }
        if (!StringUtils.isEmpty(dataArrayJson)){
            List<EleinspectPointManage> pointList = JSONArray.parseArray(dataArrayJson, EleinspectPointManage.class);
            if (pointList.size() > 0){
//                int i = 0;
//                int j = 0;
                int pointNum = 0;
                if (!StringUtils.isEmpty(eleinspectLineManage.getLineId())){
                    eleinspectMappingPointLineService.deleteByLineId(eleinspectLineManage.getLineId());
                }
                for (EleinspectPointManage eleinspectPointManage : pointList){
                    if (eleinspectPointManage == null){
                        continue;
                    }else{
                        //线路巡查时间
//                        i = i + eleinspectPointManage.getPointInspectTime();
                        //线路巡查误差时间
//                        j = j + eleinspectPointManage.getPointInspectErrorTime();
                        //点位数
                        pointNum++;
                        //保存线路-点位信息
                        EleinspectMappingPointLine eleinspectMappingPointLine = new EleinspectMappingPointLine();
                        eleinspectMappingPointLine.setMappingLineId(eleinspectLineManage.getLineId());
                        eleinspectMappingPointLine.setMappingPointId(eleinspectPointManage.getPointId());
                        eleinspectMappingPointLine.setMappingCreateTime(new Date());

                        eleinspectMappingPointLineService.insert(eleinspectMappingPointLine);

                        //更改点位状态
//                        EleinspectPointManage inspectPointManage = eleinspectPointManageService.selectByKey(eleinspectPointManage.getPointId());
//                        inspectPointManage.setPointUsedStatus(1);
//                        eleinspectPointManageService.updateByKey(inspectPointManage);
                    }
                }
//                if (i == 0 && j == 0){
//                    resultMap.put("code",204);
//                    resultMap.put("msg","请添加点位");
//                    return resultMap;
//                }
                if (StringUtils.isEmpty(firstInspectTime)){
                    eleinspectLineManage.setLineFirstInspectTime("随时");
                }else{
                    eleinspectLineManage.setLineFirstInspectTime(firstInspectTime);
                }
//                eleinspectLineManage.setLineInspectTime(i);
//                eleinspectLineManage.setLineInspectErrorTime(j);
                eleinspectLineManage.setLinePointNumber(pointNum);
                if (editIndex == 0){
                    eleinspectLineManage.setLineCreateTime(new Date());
                }
                if (editIndex == 1){
                    eleinspectLineManageService.updateByKey(eleinspectLineManage);
                }else if (editIndex == 0){
                    eleinspectLineManageService.insert(eleinspectLineManage);
                }

                //被移除的点位，更改点位状态
//                if (!StringUtils.isEmpty(deletePointIdJson)){
//                    List<String> deletePointIdList = JSONArray.parseArray(deletePointIdJson, String.class);
//                    if (deletePointIdList.size() > 0){
//                        for (String pointId : deletePointIdList) {
//                            eleinspectMappingPointLineService.deleteByPointId(pointId);
//                            //更改点位状态
//                            EleinspectPointManage eleinspectPointManage = eleinspectPointManageService.selectByKey(pointId);
//                            eleinspectPointManage.setPointUsedStatus(0);
//                            eleinspectPointManageService.updateByKey(eleinspectPointManage);
//                        }
//                    }
//                }
            }else{
                resultMap.put("code",204);
                resultMap.put("msg","请添加点位");
                return resultMap;
            }

        }
        resultMap.put("code",0);
        resultMap.put("msg","操作成功");
        return resultMap;
    }

    @RequestMapping("/deleteLine")
    public Map<String,Object> deleteLine(String lineId){
        HashMap<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("lineId",lineId);
        //修改点位状态
//        List<EleinspectMappingPointLine> list = eleinspectMappingPointLineService.list(param);
//        for (EleinspectMappingPointLine eleinspectMappingPointLine : list) {
//            EleinspectPointManage eleinspectPointManage = eleinspectPointManageService.selectByKey(eleinspectMappingPointLine.getMappingPointId());
//            eleinspectPointManage.setPointUsedStatus(0);
//            eleinspectPointManageService.updateByKey(eleinspectPointManage);
//        }
        //删除线路-点位信息
        eleinspectMappingPointLineService.deleteByLineId(lineId);
        //删除线路信息
        eleinspectLineManageService.deleteByKey(lineId);
        resultMap.put("code",0);
        resultMap.put("msg","删除成功");
        return resultMap;
    }

    @RequestMapping("/deleteAll")
    public Map<String,Object> deleteAll(String lineIds){
        HashMap<String,Object> resultMap = new HashMap<>();
        if (!StringUtils.isEmpty(lineIds)){
            List<String> lineIdsList = JSONArray.parseArray(lineIds, String.class);
            eleinspectLineManageService.deleteByLineIds(lineIdsList);
        }
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        return resultMap;
    }

    @RequestMapping("/getLinePointInfo")
    public Map<String,Object> getLinePointInfo(String mappingLineId){
        HashMap<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("mappingLineId",mappingLineId);
        List<EleinspectPointManage> pointList = new ArrayList<>();
        List<EleinspectMappingPointLine> LinePointlist = eleinspectMappingPointLineService.list(param);
        for(EleinspectMappingPointLine eleinspectMappingPointLine: LinePointlist){
            EleinspectPointManage eleinspectPointManage = eleinspectPointManageService.selectByKey(eleinspectMappingPointLine.getMappingPointId());
            pointList.add(eleinspectPointManage);
        }
        resultMap.put("code",0);
        resultMap.put("data",pointList);
        resultMap.put("msg","请求成功");
        return resultMap;
    }

}
